package com.liip.platform.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.liip.platform.common.ReturnT;
import com.liip.platform.mapper.BaseMapper;
import com.liip.platform.service.BaseService;
/**
 * @description 基础服务实现类
 * @author 罗林
 * @date 2020-03-25 17:06:18
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

	public abstract BaseMapper<T> getMapper();

	@Override
	public ReturnT<String> insert(T t) {
		// valid
		if (t == null) {
			return new ReturnT<String>(ReturnT.FAIL_CODE, "必要参数缺失");
		}

		int ret = getMapper().insert(t);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	@Override
	public ReturnT<String> delete(int id) {
		int ret = getMapper().delete(id);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	@Override
	public ReturnT<String> update(T t) {
		int ret = getMapper().update(t);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

	@Override
	public T load(int id) {
		return getMapper().load(id);
	}

	@Override
	public Map<String, Object> pageList(int offSet, int pageSize) {
		List<T> pageList = getMapper().pageList(offSet, pageSize);
		int totalCount = getMapper().pageListCount(offSet, pageSize);
		// result
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("pageList", pageList);
		result.put("totalCount", totalCount);
		return result;
	}

	@Override
	public ReturnT<String> insertForeach(List<T> list) {
		int ret = getMapper().insertForeach(list);
		return ret>0?ReturnT.SUCCESS:ReturnT.FAIL;
	}

}
